package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.base.ResponseResult;
import com.zzyl.dto.NursingLevelDto;
import com.zzyl.entity.NursingLevel;
import com.zzyl.enums.BasicEnum;
import com.zzyl.exception.BaseException;
import com.zzyl.mapper.NursingLevelMapper;
import com.zzyl.service.NursingLevelService;
import com.zzyl.service.NursingPlanService;
import com.zzyl.vo.NursingLevelVo;
import com.zzyl.vo.NursingPlanVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class NursingLevelServiceImpl implements NursingLevelService {
    @Autowired
    private NursingLevelMapper nursingLevelMapper;
    @Autowired
    private NursingPlanService nursingPlanService;

    @Override
    public List<NursingLevelVo> selectAllNursingLevel() {
        List<NursingLevelVo> vo = nursingLevelMapper.getAllNursingLevels();
        return vo;
    }

    @Override
    public PageResponse<NursingLevelVo> selectQuery(Integer status, String name, Integer pageNum, Integer pageSize) {
        //分页
        PageHelper.startPage(pageNum, pageSize);
        //调用Mapper层
        Page<NursingLevelVo> nursingLevelVoPage = nursingLevelMapper.selectQuery(status, name);
        return PageResponse.of(nursingLevelVoPage, NursingLevelVo.class);
    }

    @Override
    public void insertNursingLevel(NursingLevelDto dto) {
        //dto转换entity
        NursingLevel nursingLevel = BeanUtil.toBean(dto, NursingLevel.class);
        //添加之前做一下判断name是否已经存在
        Integer count = nursingLevelMapper.selectNursingLevelByName(nursingLevel.getName());
        if(count > 0){
            throw new BaseException(BasicEnum.NURSING_LEVEL_NAME);
        }
        //调用Mapper层添加护理等级
        nursingLevelMapper.insertNursingLevel(nursingLevel);
    }

    @Override
    public NursingLevelVo selectNursingLevelById(Long id) {
        //查询护理等级
        NursingLevel nursingLevel = nursingLevelMapper.selectNursingLevelById(id);
        //查询护理等级相关联的护理计划
        NursingPlanVo nursingPlanVo = nursingPlanService.selectNursingPlanById(nursingLevel.getPlanId());
        nursingLevel.setPlanName(nursingPlanVo.getPlanName());
        return BeanUtil.toBean(nursingLevel, NursingLevelVo.class);
    }

    @Override
    public void updateNursingLevel(NursingLevelDto dto) {
        //dto转换entity
        NursingLevel nursingLevel = BeanUtil.toBean(dto, NursingLevel.class);
        //查询修改之后的护理计划名称是否重复
        Integer count = nursingLevelMapper.selectNursingLevel(nursingLevel.getName(),nursingLevel.getId());
        if(count > 0){
            throw new BaseException(BasicEnum.NURSING_PROJECT_NAME);
        }
        //修改护理护理等级
        nursingLevelMapper.updateNursingLevel(nursingLevel);
    }

    @Override
    public void deleteNursingLevelById(Long id) {
        nursingLevelMapper.deleteNursingLevelById(id);
    }

    @Override
    public void updateStatus(Long id, Integer status) {
        nursingLevelMapper.updateStatus(id, status);
    }
}
